﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

/*
Windows系统不区分大小写,
Linux和Mac需要区分
对于轻量级的文件读写可以使用File提供的read/write方法,但是当读写文本数据复杂后得考虑使用流来读写了
*/
class Program
{     
    static void Main(string[] args)
    {
        string path = @"D:\My\.Net Project\文件操作\File类操作\MyFile.txt";

        /*基础操作*/
        //Exists(path)
        //Delete(path)

        /*读取text操作*/
        //ReadAllText(path);
        //ReadAllLine(path);
        //ReadLines(path);

        /*写入text操作*/
        //WriteAllText(path);
        //WriteAllLine(path);
        //AppendAllLines(path);

        /*读写byte数据*/
        //ReadAllBytes(path);
        //WriteAllBytes(path);

        Console.ReadKey();
    }

    /// <summary>
    /// 路径上是否有这个文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    static bool Exists(string path)
    {
        return File.Exists(path);
    }  
    /// <summary>
    /// 删除指定路径的文本
    /// </summary>
    /// <param name="path"></param>
    static void Delete(string path)
    {
        File.Delete(path);
    }
    /// <summary>
    /// 读取所有的文本,返回值是string
    /// </summary>
    /// <param name="path"></param>
    static void ReadAllText(string path)
    {                                       //不写后面的参数则就是默认Utf-8编码
        string Text = File.ReadAllText(path,Encoding.GetEncoding("Utf-8"));
        Console.WriteLine(Text);
    }
    /// <summary>
    /// 读取所有文本,自动以每行分割成string数组,范围值是string[]
    /// </summary>
    /// <param name="path"></param>
    static void ReadAllLine(string path)
    {
        string[] TextB = File.ReadAllLines(path);
        for (int i = 0; i < TextB.Length; i++)
        {
            Console.WriteLine(TextB[i]);
        }
    }
    /// <summary>
    /// 读取所有文本,每行作为迭代对象
    /// </summary>
    /// <param name="path"></param>
    static void ReadLines(string path)
    {
        foreach (string line in File.ReadLines(path))
        {
            if (line.Contains("episode") & line.Contains("2006"))
            {
                Console.WriteLine(line);
            }
        }
    }
    /// <summary>
    /// 指定文本覆盖添加文字(不会换行)
    /// </summary>
    /// <param name="path"></param>
    static void WriteAllText(string path)
    {
        File.WriteAllText(path, "龙星爆炎霸,ics-Frozen");
    }
    /// <summary>
    /// 指定文本覆盖添加新文字(根据string[]每个元素换一个行)
    /// </summary>
    /// <param name="path"></param>
    static void WriteAllLine(string path)
    {
        string[] text = new string[3]
        {
            "龙炎烈焰爆",
            "神之尺",
            "Frozen Nova"
        }; 
        File.WriteAllLines(path, text);
        ReadAllLine(path);
    }
    /// <summary>
    /// 给制定路径下的文本添加新的文字
    /// </summary>
    /// <param name="path"></param>
    static void AppendAllLines(string path)
    {
        List<string> strList = new List<string>();
        strList.Add("DEF:897");
        File.AppendAllLines(path, strList);

        //随便用那个读取方法都行
        ReadAllText(path);
        //ReadAllLine(path);
    } 
    /// <summary>
    /// 读取所有的字节(这个方法什么都能读,因为存储到电脑最终都会转换为字节)
    /// </summary>
    /// <param name="path"></param>
    static void ReadAllBytes(string path)
    {
        byte[] arr;
        arr = File.ReadAllBytes(path);
        //将字节数据转为二进制
        foreach (var abyte in arr)
        {                              //转换为二进制数据  补位,以免少掉第一位
            Console.WriteLine(Convert.ToString(abyte, 2).PadLeft(8,'0'));
        }
        //将字节数据转为文字
        string text = Encoding.UTF8.GetString(arr);
        ReadAllText(text);
    }
    /// <summary>
    /// 写入指定位置的byte数据
    /// </summary>
    /// <param name="path"></param>
    static void WriteAllBytes(string path)
    {
        byte[] bytes = File.ReadAllBytes(path);
        File.WriteAllBytes(path, bytes);             
    }
}